% cm_print_state.m
% 一个标准化的日志打印函数，用于清晰地展示 CacheManager 的内部状态。

function cm_print_state(prefix, step, node_id, op_name, buffer_id, type_code, manager, timing)
    % 打印一个清晰的头部信息
    fprintf('\n--- [%s LOG | Step: %d | Node: %d | Op: %s | BufID: %d | TypeCode: %d] --- (%s)\n', ...
            upper(prefix), step, node_id, op_name, buffer_id, type_code, upper(timing));
    
    % 打印已分配的块
    fprintf('  Allocated Blocks (%d):\n', length(manager.allocated_blocks));
    if isempty(manager.allocated_blocks)
        fprintf('    (None)\n');
    else
        for i = 1:length(manager.allocated_blocks)
            blk = manager.allocated_blocks(i);
            fprintf('    - BufID: %-5d | Start: %-5d | Size: %d\n', blk.buf_id, blk.start, blk.size);
        end
    end

    % 打印空闲的块
    fprintf('  Free Blocks (%d):\n', length(manager.free_blocks));
    if isempty(manager.free_blocks)
        fprintf('    (None)\n');
    else
        for i = 1:length(manager.free_blocks)
            blk = manager.free_blocks(i);
            fprintf('    - Start: %-5d | Size: %d\n', blk.start, blk.size);
        end
    end
    fprintf('-----------------------------------------------------------------------------------\n');
end